package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author :ligenghuisheng
 * @Description:
 * @Date :2022/4/22  16:45
 */
@RestController
public class ShoppingCartController {


    @Autowired
    ICartService iCartService;


    @GetMapping("/shopping/carts")
    //获得购物车列表
    public ResponseData showShopList(HttpServletRequest request){
        String user_info = request.getHeader("user_info");
        //System.out.println(token);
        Map<String, Object> map = JSON.parseObject(user_info, Map.class);
        //JSONObject jsonObject = JSON.parseObject(token);

        Integer userId = (Integer) map.get("id");
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(userId);
        CartListByIdResponse cartListById = iCartService.getCartListById(cartListByIdRequest);

        if(ShoppingRetCode.SUCCESS.getCode().equals(cartListById.getCode())){

           // return new ResponseUtil<>.setData(cartListById.getCartProductDtos());
            return new ResponseUtil().setData(cartListById.getCartProductDtos());
        }


        return new ResponseUtil().setErrorMsg(cartListById.getMsg());
    }

    @PostMapping("/shopping/carts")
    //添加商品到购物车
    public ResponseData addShopCarts(@RequestBody AddCartRequest addCartRequest){



        AddCartResponse addCartResponse = iCartService.addToCart(addCartRequest);
        if(ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())){

            return new ResponseUtil<String>().setData("成功");
        }

        return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
    }
    //更新购物车数量
    @RequestMapping(value = "/shopping/carts",method = RequestMethod.PUT)
    public ResponseData updateShopCarts(@RequestBody MyUpdateCarNumberRequest myUpdateCarNumberRequest){
        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(myUpdateCarNumberRequest.getUserId());
        updateCartNumRequest.setChecked(myUpdateCarNumberRequest.getChecked());
        updateCartNumRequest.setNum(myUpdateCarNumberRequest.getProductNum());
        updateCartNumRequest.setItemId(myUpdateCarNumberRequest.getProductId());


        UpdateCartNumResponse updateCartNumResponse = iCartService.updateCartNum(updateCartNumRequest);
        if(ShoppingRetCode.SUCCESS.getCode().equals(updateCartNumResponse.getCode())){

            return new ResponseUtil<String>().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());


    }

    //删除购物车
    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteShopCart(@PathVariable("uid") Long uid,@PathVariable("pid") Long pid){

        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setItemId(pid);
        deleteCartItemRequest.setUserId(uid);
        DeleteCartItemResponse deleteCartItemResponse = iCartService.deleteCartItem(deleteCartItemRequest);
        if(ShoppingRetCode.SUCCESS.getCode().equals(deleteCartItemResponse.getCode())){
            return new ResponseUtil<String>().setData("成功");


        }
        return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());


    }
    //删除选中的商品
    @DeleteMapping("shopping/items/{uid}")
    public ResponseData shoppingItemsDelete(@PathVariable("uid") Long uid) {
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);

        DeleteCheckedItemResposne deleteCheckedItemResposne = iCartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCheckedItemResposne.getCode())) {
            return new ResponseUtil<String>().setData("成功");
        }
        return new ResponseUtil<String>().setErrorMsg(deleteCheckedItemResposne.getMsg());
    }

    //选中所有商品
    @RequestMapping(value = "/shopping/items",method = RequestMethod.PUT)
    public ResponseData selectAllShopping(@RequestBody CheckAllItemRequest checkAllItemRequest){

        CheckAllItemResponse checkAllItemResponse = iCartService.checkAllCartItem(checkAllItemRequest);
        if(ShoppingRetCode.SUCCESS.getCode().equals(checkAllItemResponse.getCode())){
            return new ResponseUtil<String>().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(checkAllItemResponse.getMsg());
    }


}
